/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/16 11:50
 * @Copyright
 */
package JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

//将连接数据库的所有信息配置到配置文件中
/*
实际开发中不建议把连接数据库的信息写死到Java程序中
 */
public class JDBCtest4 {
    public static void main(String[] args) {
        //使用资源绑定器属性配置文件
        ResourceBundle bundle = ResourceBundle.getBundle("JDBC/jdbc");
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");

        Connection conn = null;
        Statement stmt = null;

        try {
            //1.注册驱动
            Class.forName(driver);
            //2.获得连接
            conn = DriverManager.getConnection(url, user, password);
            //3.获取数据库操作对象
            stmt = conn.createStatement();
            //4.执行SQL
            String sql = "update user set username='张三' where id='5'";
            int count = stmt.executeUpdate(sql);
            System.out.println(count);
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            //第六步：释放资源
            try{
                if (stmt != null){
                    stmt.close();
                }
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
            try{
                if (conn != null) {
                    conn.close();
                }
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
        }
    }
}

